package it.sd.pwframework.business.service.crud.impl;

import it.sd.pwframework.business.service.crud.CrudService;
import it.sd.pwframework.persistence.common.CommonSession;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

public abstract class AbstractCrudService<K> implements CrudService<K>{

    @Autowired
    private CommonSession commonSession;
	
	protected Class<K> entityClass;
	
	public List<K> listByProperites(Map<String, Object> props) {
		@SuppressWarnings("unchecked")
		List<K> lista = commonSession.findByProperties(entityClass.getClass(), props);
		return lista;
	}

	public K getByProperites(Map<String, Object> props) {
		List<K> lista = listByProperites(props);
		if (lista.isEmpty()) {
			return null;
		} else {
			return lista.get(0);
		}
	}
	
	public void saveOrUpdate(K k) throws Exception {
		commonSession.saveOrUpdate(k);
	}
	

}
